//
//  HMEmoticonKeyboardViewModel.swift
//  swift-新浪微博0
//
//  Created by 赵玉忠 on 16/9/10.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

/// 1页里面的表情个数
let HMEmoticonKeyboardPageEmoticonCount = 20
class HMEmoticonKeyboardViewModel: NSObject {
    
    static let sharedViewModel = HMEmoticonKeyboardViewModel()
    
    lazy var emoticonBundel: Bundle = {
        let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
        // 初始化一个bundle
        let result = Bundle(path: path)!
        return result
    }()
    
    /// 最近表情的数组
    lazy var recentEmotions: [HMEmoticon] = {
        return [HMEmoticon]()
    }()
    
    /// 默认表情的数组
    lazy var defaultEmotions: [HMEmoticon] = {
        return self.emoticons(path: "default/info.plist")
    }()
    
    /// emoji表情的数组
    lazy var emojiEmotions: [HMEmoticon] = {
        return self.emoticons(path: "emoji/info.plist")
    }()


    // 浪小花表情的数组
    lazy var lxhEmotions: [HMEmoticon] = {
        return self.emoticons(path: "lxh/info.plist")
    }()
    
    /// 提供给外界使用的表情数据，是一个三维数组类型
    lazy var allEmotions: [[[HMEmoticon]]] = {
        //        return [
        //        最近表情的二维数组,
        //        默认表情的二维数组,
        //        emoji表情的二维数组,
        //        lxh表情的二维数组,
        //        ]
        return [
            [self.recentEmotions],
            self.typeEmotionPages(emoticons: self.defaultEmotions),
            self.typeEmotionPages(emoticons: self.emojiEmotions),
            self.typeEmotionPages(emoticons: self.lxhEmotions)
        ]
        
    }()

    
    override init() {
        super.init()
        
        print(emoticonBundel)
    }
    
    /// 将一类的表情数组，分割成20个表情一页的表情数组
    ///
    /// - parameter emoticons: <#emoticons description#>
    ///
    /// - returns: <#return value description#>
    private func typeEmotionPages(emoticons: [HMEmoticon]) -> [[HMEmoticon]] {
        
        // 假如：传入的emoticons是108个
        
        // 1. 要知道传入的表情可以分成多少页
        let pageCount = (emoticons.count - 1) / HMEmoticonKeyboardPageEmoticonCount + 1
        
        var result = [[HMEmoticon]]()
        
        for i in 0..<pageCount {
            
            // 假如i为0.要截取0-19
            // 假如i为1.要截取20-39
            
            // loc指从哪个位置开始截取
            let loc = i * HMEmoticonKeyboardPageEmoticonCount
            // len代表要截取的长度
            var len = HMEmoticonKeyboardPageEmoticonCount
            
            // 判断开始截取的位置加上截取的长度是否超过总长度
            if loc + len > emoticons.count {
                // 如果超过，那么就使用总长度减去开始截取的位置
                len = emoticons.count - loc
            }
            
            // 指定范围
            let range = NSMakeRange(loc, len)
            // 从大数组里面截取指定范围的数组
            let subArray = (emoticons as NSArray).subarray(with: range) as! [HMEmoticon]
            
            result.append(subArray)
        }
        
        return result
    }

    
    private func emoticons(path: String) -> [HMEmoticon] {
        // 取到指定的info.plist
        let path = self.emoticonBundel.path(forResource: path, ofType: nil)!
        // 从指定路径读取数据
        let array = NSArray(contentsOfFile: path)!
        // 进行字典转模型
        let emoticons = NSArray.yy_modelArray(with: HMEmoticon.self, json: array)! as! [HMEmoticon]
        // 设置表情模型对应的文件夹
        for value in emoticons {
            let p = (path as NSString).deletingLastPathComponent
            value.path = p
        }

        return emoticons
    }
    
}
